package com.sad.jetpack.v1.datamodel.api.extension.client.socket.ipc;

import android.text.TextUtils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.nio.charset.StandardCharsets;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;

public class IPCChatHeader {
    private String id="";//会话id
    private int version=1;//会话版本
    private IPCMember from=IPCMember.newInstance();//会话来源
    private IPCChatIntent intent=new IPCChatIntent();//会话意图
    private List<IPCMember> to=new ArrayList<>();//会话发送目标
    private boolean toAll=false;
    private String time="";//会话发送时间
    private String description="";//会话描述
    private JSONObject extra=new JSONObject();//附加信息
    private String signature="";//服务端验证合法性的签名

    public String getSignature() {
        return signature;
    }

    public void setSignature(String signature) {
        this.signature = signature;
    }

    public IPCChatIntent getIntent() {
        return intent;
    }

    public void setIntent(IPCChatIntent intent) {
        this.intent = intent;
    }

    public boolean isToAll() {
        return toAll;
    }

    public void setToAll(boolean toAll) {
        this.toAll = toAll;
    }

    public int getVersion() {
        return version;
    }

    public void setVersion(int version) {
        this.version = version;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public IPCMember getFrom() {
        return from;
    }

    public void setFrom(IPCMember from) {
        this.from = from;
    }

    public List<IPCMember> getTo() {
        return to;
    }

    public void setTo(List<IPCMember> to) {
        this.to = to;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public JSONObject getExtra() {
        return extra;
    }

    public void setExtra(JSONObject extra) {
        this.extra = extra;
    }
    public static IPCChatHeader parse(String s){
        return parse(s.getBytes(StandardCharsets.UTF_8));
    }
    public static IPCChatHeader parse(byte[] bytes){
        String s=new String(bytes,StandardCharsets.UTF_8);
        try {

            IPCChatHeader header=new IPCChatHeader();
            JSONObject json=new JSONObject(s);
            header.setId(json.optString("id"));
            header.setVersion(json.optInt("version",1));
            header.setFrom(IPCMember.parse(json.optJSONObject("from")));
            header.setIntent(IPCChatIntent.parse(json.optJSONObject("intent")));
            header.setDescription(json.optString("description"));
            header.setTime(json.optString("time"));
            //header.setBodySize(json.optLong("bodySize"));
            header.setExtra(json.optJSONObject("extra"));
            JSONArray ja=json.optJSONArray("to");
            List<IPCMember> toMembers=new ArrayList<>();
            for (int i = 0; i < ja.length(); i++) {
                JSONObject j=ja.optJSONObject(i);
                IPCMember toMember=IPCMember.parse(j);
                toMembers.add(toMember);
            }
            header.setTo(toMembers);
            header.setToAll(json.optBoolean("toAll"));
            return header;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public JSONObject toJson(){
        try {
            JSONObject jsonObject=new JSONObject();
            jsonObject.put("id",getId());
            jsonObject.put("version",getVersion());
            jsonObject.put("from",getFrom().toJson());
            jsonObject.put("intent",getIntent().toJson());
            JSONArray ja_to=new JSONArray();
            for (IPCMember toMember:getTo()
                 ) {
                ja_to.put(toMember.toJson());
            }
            jsonObject.put("to",ja_to);
            jsonObject.put("toAll",isToAll());
            jsonObject.put("description",getDescription());
            jsonObject.put("time",getTime());
            //jsonObject.put("bodySize",getBodySize());
            jsonObject.put("extra",getExtra());
            return jsonObject;
        }catch (Exception exception){
            exception.printStackTrace();
        }
        return null;
    }

    /*@Deprecated
    public byte[] getWholeHeaderBytes(){
        //处理头信息
        byte[] headerMarkBytes=HEADER_MARK.getBytes();
        byte[] headerSizeMarkBytes=new byte[]{};
        int headerSizeMarkLength=HEAD_SIZE_MARK_LENGTH;
        byte[] headerBytes=new byte[]{};
        int headerSize=0;

        JSONObject h_json= toJson();
        if (h_json!=null){
            String h_str=h_json.toString();
            if (!TextUtils.isEmpty(h_str)){
                headerBytes=h_str.getBytes();
                headerSize=headerBytes.length;
                NumberFormat nbf_header = NumberFormat.getNumberInstance();
                nbf_header.setMinimumIntegerDigits(headerSizeMarkLength);
                nbf_header.setGroupingUsed(false);
                String hs=nbf_header.format(headerSize);
                headerSizeMarkBytes=hs.getBytes();
            }
        }
        byte[] messageBytes=new byte[headerMarkBytes.length+headerSizeMarkBytes.length+headerSize];
        System.arraycopy(headerMarkBytes, 0, messageBytes, 0, headerMarkBytes.length);
        System.arraycopy(headerSizeMarkBytes, 0, messageBytes, headerMarkBytes.length,headerSizeMarkBytes.length);
        System.arraycopy(headerBytes, 0, messageBytes, headerMarkBytes.length+headerSizeMarkBytes.length,headerBytes.length);
        return messageBytes;
    }*/
}
